
//图片颜色处理
function imgcolor(state, canvas, img) {
	ctx = canvas.getContext("2d");
	ctx.clearRect(0, 0, canvas.width, canvas.height);
	ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
	var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
	var data = imgData.data;
	switch (state) {
		case "definition":
			/*颜色反转*/
			for (var i = 0; i < data.length; i += 4) {
				data[i] = 255 - data[i];
				data[i + 1] = 255 - data[i + 1];
				data[i + 2] = 255 - data[i + 2];
			}

			break;
		case "black":

			/*黑白效果(灰度图)*/
			for (var i = 0; i < data.length; i += 4) {
				/*加权平均数
				var average = (data[i+0]*0.3+data[i+1]*0.6+data[i+2]*0.1)/3;
				*/
				var average = (data[i + 0] + data[i + 1] + data[i + 2]) / 3;
				data[i + 0] = average;
				data[i + 1] = average;
				data[i + 2] = average;
			}

			break;
		case "light":

			/*增强亮度*/
			for (var i = 0; i < data.length; i += 4) {
				var x = 20;
				data[i + 0] += x;
				data[i + 1] += x;
				data[i + 2] += x;
			}

			break;
		case "retro":

			/*复古效果*/
			for (var i = 0; i < data.length; i += 4) {
				var r = data[i + 0];
				var g = data[i + 1];
				var b = data[i + 2];
				data[i + 0] = r * 0.39 + g * 0.76 + b * 0.18;
				data[i + 1] = r * 0.35 + g * 0.68 + b * 0.16;
				data[i + 2] = r * 0.27 + g * 0.53 + b * 0.13;
			}

			break;
		case "red":

			/*红色蒙版*/
			for (var i = 0; i < data.length; i += 4) {
				var r = data[i + 0];
				var g = data[i + 1];
				var b = data[i + 2];
				var average = (r + g + b) / 3;
				data[i + 0] = average;
				data[i + 1] = 0;
				data[i + 2] = 0;
			}

			break;
		case "alpha":

			/*透明效果*/
			for (var i = 0; i < data.length; i += 4) {
				data[i + 3] = data[i + 3] * 0.5;
			}
			break;
		default: console.log('imgcolor null')
	}

	ctx.putImageData(imgData, 0, 0);
}